﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.BOM
{
    public partial class BOMChangeView : AuthBasePage
    {
        public DataTable BOMChangeMaterialDT
        {
            set { Session["BOMChangeMaterialDT"] = value; }
            get
            {
                if (Session["BOMChangeMaterialDT"] == null)
                {
                    return CreateDt();
                }
                else
                {
                    return (DataTable)Session["BOMChangeMaterialDT"];
                }
            }
        }
        public DataTable CreateDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BOMChangeDetailID");
            dt.Columns.Add("BOMDetailID");
            dt.Columns.Add("MaterialID");//原料编号
            dt.Columns.Add("MaterialName");//原料名称
            dt.Columns.Add("Specifications");//规格
            dt.Columns.Add("Unit");//单位
            dt.Columns.Add("ChangeType");//变更类别
            dt.Columns.Add("Nums");//数量
            dt.Columns.Add("Remark");//备注
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BOMChangeMaterialDT = null;
                InitData();
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void InitData()
        {
            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            if (!string.IsNullOrEmpty(id))
            {
                DataTable dt = BsBOMChangeBusiness.GetDataById(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    lblBOMChangeID.Text = CommonMethod.GetString(dr["BOMChangeID"]);
                    lblBillDate.Text = string.Format("{0:yyyy-MM-dd}", dr["BillDate"]);
                    txtProductID.Text = CommonMethod.GetString(dr["ProductID"]);
                    txtProductName.Text = CommonMethod.GetString(dr["ProductName"]);
                    txtUnit.Text = CommonMethod.GetString(dr["Unit"]);

                    DataTable dtemp = BOMChangeMaterialDT.Copy();
                    foreach (DataRow drow in dt.Rows)
                    {
                        DataRow d = dtemp.NewRow();
                        d["BOMChangeDetailID"] = CommonMethod.GetString(dr["BOMChangeDetailID"]);
                        d["BOMDetailID"] = CommonMethod.GetString(dr["BOMDetailID"]);
                        d["MaterialID"] = CommonMethod.GetString(dr["MaterialID"]);
                        d["MaterialName"] = CommonMethod.GetString(dr["MaterialName"]);
                        d["Specifications"] = CommonMethod.GetString(dr["Specifications"]);
                        d["Unit"] = CommonMethod.GetString(dr["Unit"]);
                        d["ChangeType"] = CommonMethod.GetString(dr["ChangeTypeName"]);
                        d["Nums"] = CommonMethod.GetString(dr["Nums"]);
                        d["Remark"] = CommonMethod.GetString(dr["Remark"]);
                        d["IsNew"] = "-1";
                        d["IsDel"] = "-1";
                        dtemp.Rows.Add(d);
                    }
                    BOMChangeMaterialDT = dtemp.Copy();
                }
            }
            BindMaterial();
        }

        private void BindMaterial()
        {
            DataView dv = new DataView(BOMChangeMaterialDT);
            dv.RowFilter = " IsDel <> '1' ";
            DataTable dt = dv.ToTable();
            dt.Columns.Add("OrderNum");
            if (dt != null && dt.Rows.Count > 0)
            {
                int index = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    dr["OrderNum"] = index;
                    index++;
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.UpdatePanel4, "iframeAutoFit();");
        }

    }
}